//
//  NSObject+HDRepairManager.m
//  app
//
//  Created by D on 15/8/31.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDRepairManager.h"
#import "HDJiamubiaoModel.h"

@interface HDRepairManager ()

@property (nonatomic,strong) JKHttpClient * jkHttp;
@property (nonatomic,strong) SNUniteUrl * uniteUrl;
@property (nonatomic,strong) NSMutableArray * mutableArray;

@property (nonatomic,strong) NSMutableArray * jiamubiao_array;

@end

@implementation HDRepairManager

+(HDRepairManager *)shareManager
{
    AS_SINGLETON(HDRepairManager, manager, {
        
        manager = [[HDRepairManager alloc]init];
        manager.jkHttp = [[JKHttpClient alloc]init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
        manager.mutableArray = [NSMutableArray array];
        manager.jiamubiao_array = [NSMutableArray array];
    
    })
    return manager;
}

-(void)loadRepairListData:(HDRepairListResult*)result Params:(NSDictionary*)params page:(int)page
{
    if ([self.jkHttp isRunning]) {
        return;
    }
    
    
    
    NSString * url = [NSString stringWithFormat:@"http://mylife114.com/android_oa/app_repair_apply_list?mall_com_id=%@&add_code=%@&page=%@",[params objectForKey:@"mall_com_id"],[params objectForKey:@"add_code"],[NSString stringWithFormat:@"%d",page]] ;
    
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [self.jkHttp get:url result:^(JKResponse *resp) {
        
        if (resp.OK) {
            result.success = YES;
            result.failure = NO;
           
            result.dataSourceArray = [self repairDataSourceWithArray:resp.content[@"result"] page:page result:result];
            
        } else {
            result.failure = YES;
            result.success = NO;
            result.message = resp.message;
        }
        [result invoke];
        
    }];
    
}
-(NSMutableArray*)repairDataSourceWithArray:(NSArray*)array page:(int)page result:(HDRepairListResult *)result;
{
    if (page == 1) {
        [self.mutableArray removeAllObjects];
    }
    
    for (int i = 0 ; i < array.count; i++) {
        NSDictionary * dict = [array objectAtIndex:i];
        HDRepairListModle * model = [HDRepairListModle modelWithDict:dict];
//        DLog(@"com_id ====== %@",model.com_id);
        [self.mutableArray addObject:model];
        
    }
  
    result.noMoreData = array.count < 10;
    result.noData = !self.mutableArray.count;
    
    return self.mutableArray;
}


-(void)loadJiamubiaoData:(HDRepairListJiamubiaoResult *)result Params:(NSDictionary *)params
{
    if ([self.jkHttp isRunning]) {
        return;
    }
    
    
    
    NSString * url = @"http://mylife114.com/android_oa/info_repair_list";
    
    [self.jkHttp post:url params:params result:^(JKResponse *resp) {
        
        if (resp.OK) {
            result.success = YES;
            result.failure = NO;
            
            result.dataSourceArray = [self jiamubDataSourceWithArray:resp.content[@"result"]];
            
        } else {
            result.failure = YES;
            result.success = NO;
            result.message = resp.message;
        }
        [result invoke];
        
    }];

}


-(NSMutableArray*)jiamubDataSourceWithArray:(NSArray*)array
{
    
    [self.jiamubiao_array removeAllObjects];
    
    for (int i = 0 ; i < array.count; i++) {
        NSDictionary * dict = [array objectAtIndex:i];
        HDJiamubiaoModel * model = [HDJiamubiaoModel modelWithDict:dict];
        [self.jiamubiao_array addObject:model];
        
    }
    
    return self.jiamubiao_array;
    
}

@end






